<?php
namespace Frieza\Modules\Recommend\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\tools\BlockType;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\Recommend\UserDislikeContent;


class RecommendController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    //预算列表
    public function index(Request $request,Content $content){
        $request = $request->all();

        $tab = new Tab();
        $tab->addLink("推荐-反馈不感兴趣" ,'/admin/recommend/index');
        return $content
            ->header('反馈列表')
            ->breadcrumb(
                ['text' => '任务列表', 'url' => '/recommend/index']
            )
            ->body($tab)->row(self::listGrid($request));
    }

    //
    public function listGrid($request = []){
        $map = [];
        if($request['addtime']['start'] && $request['addtime']['end']){
            $map['addtime[>]'] = strtotime($request['addtime']['start']) ;
            $map['addtime[<]'] = strtotime($request['addtime']['end']);
        }
        if(isset($request['obj_type']) && $request['obj_type'] != 7 ){
            $map['obj_type'] = $request['obj_type'];
        }
        if( isset($request['content_source'])&& $request['content_source']!= 3){
            $map['content_source'] = $request['content_source'];
        }

        if(isset($request['obj_id'])&& $request['obj_id']){
            $map['obj_id'] = $request['obj_id'];
        }

        if(isset($request['act_uid']) && $request['act_uid']){
            $map['act_uid'] = $request['act_uid'];
        }
        $map['dislike_reason'] = UserDislikeContent::REASON_FEEL_BAD;
        $count = UserDislikeContent::getCountByMap($map);

        $map['ORDER'] = ['id' => 'DESC'];
        $pageSize = $request["per_page"] ? : 20;
        $page = $request['page'] ? : 1;
        $map['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $data = UserDislikeContent::getList($map);

        $gridModel = new pGridDataModel($data,$count, [
            'perPage' => $request['per_page'] ? : $request['LIMIT'][1]
        ]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->dropDown('time', [
                    'addtime' => '标记时间'
                ])->betweenDatetime();
                $filter->column(1/2, function ($filter) {
                    $filter->equal('obj_type', '内容类型')->radio([
                        7 => '全部',
                        0 => '图片',
                        1 => '整屋',
                        2 => '指南',
                        5 => '空白文章',
                        63 => '话题'
                    ])->default(7);

                    $filter->equal('content_source', '是否属于小流量池')->radio([
                        3 => '全部',
                        1 => '是',
                        0 => '否'
                    ])->default(3);
                });
                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->equal('obj_id', '内容id');
                    $filter->equal('act_uid', '标记用户uid');
                });
            });

            $grid->column('id', 'id');
            $grid->column('act_uid', '操作人uid');
            $grid->column('obj_type', '内容信息')->width(300)->display(function() {
                $type = '';
                switch($this->obj_type){
                    case BlockType::TYPE_BLANK:
                         $type = "文章";
                         break;
                    case BlockType::TYPE_ARTICLE:
                        $type ='整屋';
                        break;
                    case BlockType::TYPE_TOPIC_ACTIVITY:
                        $type = "话题";
                        break;
                    case BlockType::TYPE_GUIDE:
                        $type = "指南";
                        break;
                    case BlockType::TYPE_PHOTO:
                        $type = "图片";
                        break;
                }
                return "内容id:".$this->obj_id."<br/>内容类型:".$type."<br/> 发布人uid:".$this->content_uid."<br/>内容标签:".$this->content_tag;
            });

            $grid->column('content_source', '是否小流量池')->width(300)->display(function() {
               if($this->content_source){
                   return "是";
               }else{
                   return "否";
               }
            });

            $grid->column('addtime', '标记时间')->display(function () {
                return date('Y-m-d H:i:s', $this->addtime);
            });

            $grid->disableActions();

        });
        $list = $listGrid->render();
        return $list;
    }



}